<template>
	<view class="root-wrap">
		<view v-for="item,index in list" :key="index" class="form-item">
			<view class="label">
				<text class="title">{{item.fieldName}}</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u--input
					:placeholder="`请输入${item.fieldName}`"
					border="none"
					v-model="item.fieldValue"
					fontSize="28rpx"
					:readonly="readOnly&&!canEdit"
					@change="e=>change(e,index)"
				  ></u--input>
			</view>
		</view>
		<view v-if="!readOnly || canEdit" @click="onSubmit" class="submit"><u-button color="#30D6C2" type="primary" shape="circle"><image class="icon" src="../../static/assets/imgs/quotaion-submit.png"></image><text class="label">提交报价</text></u-button></view>
	</view>
</template>

<script>
import { getProgramQuotation,saveQuotation } from '../../api/program';
	export default {
		data() {
			return {
				list:[],
				readOnly: false,
				loading: false,
				canEdit: false, // 特殊情况编辑报价
			};
		},
		onLoad(params){
			console.log(params);
			const { id,edit } = params;
			if(!id){
				uni.showToast({
					title: '项目ID错误,请退出重试'
				})
				return
			}
			if(edit){
				this.canEdit = true;
			}
			this.id = id;
			this.getDataInfo();
		},
		methods:{
			getDataInfo(){
				let that = this;
				getProgramQuotation({bid_id:that.id}).then((res)=>{
					if (1 == res.code) {
						that.list = res.data.fields;
						that.readOnly = Boolean(res.data.isQuote);
					} else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			change(e,index){
				this.list[index].filedValue = e;
			},
			onSubmit(){
				if(this.loading) return;
				
				const check = this.list.some(item=>!item.filedValue||item.filedValue=='');
				if(check){
					uni.showToast({
						title:'请检查所有必填项',
						icon:'none'
					})
					return
				}
				
				this.loading = true;
				uni.showLoading({
					title: '提交中...'
				})
				const params = {
					"bid_id": this.id,
					"fields": this.list, 
				}
				saveQuotation(params).then(res => {
					this.loading = false;
					uni.hideLoading();
					if(res.code==1){
						uni.showToast({
							title:'提交成功',
							icon:'success'
						})
						const timer = setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
							clearTimeout(timer);
						},900);
					}else{
						uni.showToast({
							title: res.msg,
							icon:'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.root-wrap{
	.form-item{
		margin: 0 24rpx;
		.label{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 44rpx 0 24rpx;
			.title{
				font-size: 28rpx;
				color: #606266;
				line-height: 50rpx;
			}
			.require{
				font-size: 28rpx;
				color: #E62020;
				line-height: 50rpx;
				margin-left: 24rpx;
			}
		}
		.input-wrap{
			width: 700rpx;
			height: 90rpx;
			background-color: #F3F4F6;
			margin: 0 auto;
			border-radius: 8rpx;
			.u-input{
				height: 100% !important;
				input{
					padding: 0 24rpx !important;
				}
			}
		}
	}
	.submit{
		width: 272rpx;
		height: 80rpx;
		margin: 428rpx auto 110rpx;
		.icon{
			width: 48rpx;
			height: 48rpx;
			margin-right: 16rpx;
		}
		.label{
			font-weight: 500;
			font-size: 32rpx;
			color: #fff;
		}
	}
}
</style>
